package com.szqhyxj.youxiangju.biz.plan.adapter;

import android.annotation.SuppressLint;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.szqhyxj.youxiangju.R;
import com.szqhyxj.youxiangju.biz.plan.RepaymentModel;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class UnBilledAdapter extends RecyclerView.Adapter<UnBilledAdapter.IconViewHolder> {


    private List<RepaymentModel.RepaymentEntity> repaymentEntityList;

    private IconClickListener iconClickListener;

    public UnBilledAdapter(List<RepaymentModel.RepaymentEntity> repaymentEntityList, IconClickListener iconClickListener) {
        this.repaymentEntityList = repaymentEntityList;
        this.iconClickListener = iconClickListener;
    }


    @NonNull
    @Override
    public IconViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new IconViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.framelayout_unbilled_item, viewGroup, false));
    }

    @SuppressLint("ResourceAsColor")
    @Override
    public void onBindViewHolder(@NonNull IconViewHolder iconViewHolder, final int i) {
        final RepaymentModel.RepaymentEntity repaymentEntity = repaymentEntityList.get(i);
        Glide.with(iconViewHolder.itemView.getContext()).load(repaymentEntity.getCardLogo()).into(iconViewHolder.bankicon);
        iconViewHolder.bankName.setText(repaymentEntity.getBtName());
        String bankcardno = repaymentEntity.getBankCardNo();
        String bankcardnosub = bankcardno.substring(bankcardno.length() - 4, bankcardno.length());
        String cardno = "(" + bankcardnosub + ")";
        iconViewHolder.bankcardNo.setText(cardno);
        String fixedamount = repaymentEntity.getFixedAmount();
        String amount = fixedamount.substring(0,fixedamount.length()-2);
        iconViewHolder.price.setText(amount);


        String str = repaymentEntity.getDetDays();
        String day = str.substring(str.indexOf("-") + 1, str.length());
        String month = str.substring(0, str.indexOf("-"));
        iconViewHolder.month.setText(month);
        iconViewHolder.day.setText(day);
        if (repaymentEntity.getBankStates() == 0) {
            iconViewHolder.certification.setText("进行报备");
            iconViewHolder.certification.setBackgroundResource(R.drawable.background_shangyibubtn);
            iconViewHolder.certification.setTextColor(R.color.invitationColor);
        }
        if (repaymentEntity.getBankStates() == 1) {
            iconViewHolder.certification.setText("计划执行中");
            iconViewHolder.certification.setBackgroundResource(R.color.white);
            iconViewHolder.certification.setTextColor(R.color.invitationColor);
        }
        if (repaymentEntity.getBankStates() == 2) {
            iconViewHolder.certification.setText("制定计划");
            iconViewHolder.certification.setBackgroundResource(R.drawable.background_feedbackbtn);
            iconViewHolder.certification.setTextColor(R.color.white);
        }
        if (repaymentEntity.getBankStates() == 3) {
            iconViewHolder.certification.setVisibility(View.GONE);
        }if (repaymentEntity.getBankStates() ==4){
            iconViewHolder.certification.setText("计划暂停中");
            iconViewHolder.certification.setBackgroundResource(R.color.white);
            iconViewHolder.certification.setTextColor(R.color.invitationColor);
        }

        iconViewHolder.certification.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                iconClickListener.onClickItem(i, repaymentEntityList);
            }
        });
    }

    @Override
    public int getItemCount() {
        return repaymentEntityList.size();
    }

    static class IconViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.bankicon)
        ImageView bankicon;
        @BindView(R.id.bankName)
        TextView bankName;
        @BindView(R.id.bankcardNo)
        TextView bankcardNo;
        @BindView(R.id.certification)
        Button certification;
        @BindView(R.id.price)
        TextView price;
        @BindView(R.id.month)
        TextView month;
        @BindView(R.id.day)
        TextView day;

        IconViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface IconClickListener {
        void onClickItem(int i, List<RepaymentModel.RepaymentEntity> repaymentEntity);
    }
}